package com.lxw.rmg.menu.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxw.common.utils.PageUtils;
import com.lxw.common.utils.R;
import com.lxw.rmg.menu.entity.NutrientTableEntity;
import com.lxw.rmg.menu.service.NutrientTableService;
import com.lxw.rmg.menu.utils.CompareUtils;
import com.lxw.rmg.menu.utils.TimeUtil;
import com.mysql.cj.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lxw.rmg.menu.entity.PersonDishEntity;
import com.lxw.rmg.menu.service.PersonDishService;




/**
 * 关联表
 *
 * @author lxw
 * @email 
 * @date 2023-03-26 02:03:13
 */
@RestController
@RequestMapping("menu/persondish")
public class PersonDishController {
    @Autowired
    private PersonDishService personDishService;
    @Autowired
    private NutrientTableService nutrientTableService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("menu:persondish:list")
    public R list(@RequestParam Map<String, Object> params) throws IllegalAccessException {

//
        PageUtils page = personDishService.queryPage(params);
        //创建一个常量类
        NutrientTableEntity nutrientTableEntity = new NutrientTableEntity();
        //根据实际情况自己设置
        nutrientTableEntity.setEnergy(50.0f);//能量
        nutrientTableEntity.setProtein(40.0f);//蛋白质
        nutrientTableEntity.setCarbohydrate(54.0f);//碳水化合物
        nutrientTableEntity.setFat(34.0f);//脂肪
        nutrientTableEntity.setSugar(89.0f);//糖
        nutrientTableEntity.setSaturatedFat(89.0f);//饱和脂肪
        nutrientTableEntity.setTransFat(89.0f);//反式脂肪
        nutrientTableEntity.setCholesterol(89.0f);//胆固醇
        nutrientTableEntity.setSodium(89.0f);//钠





        //获取菜品名称和身份证号
        String idCard = (String) params.get("idCard");

        if (StringUtils.isNullOrEmpty(idCard) || " ".equals(idCard)){
           return R.error(99,"请输入身份证");
        }


        List<PersonDishEntity> personDishs = personDishService.list(new QueryWrapper<PersonDishEntity>().eq("id_card", idCard));

        if (personDishs.size() == 0 ){
            return R.error(99,"不存在");
        }
        NutrientTableEntity nutrientTableEntity2 = new NutrientTableEntity();
        //循环遍历截取
        for (PersonDishEntity personDish : personDishs) {
            //对比当前时间
            if (TimeUtil.compareTime(personDish.getCreatedAt())){
                //如果是今天 获取菜名
                String dishName = personDish.getDishName();
                //查询菜品对应的营养
                NutrientTableEntity nutrientTable =
                        nutrientTableService.getOne(new QueryWrapper<NutrientTableEntity>().eq("dish_name", dishName));
                if (nutrientTable == null){
                    continue;
                }
                //根据实际情况自己设置
                nutrientTableEntity2.setEnergy(nutrientTableEntity2.getEnergy() +nutrientTable.getEnergy());//能量
                nutrientTableEntity2.setProtein(nutrientTableEntity2.getProtein() +nutrientTable.getProtein());//蛋白质
                nutrientTableEntity2.setCarbohydrate(nutrientTableEntity2.getCarbohydrate() +nutrientTable.getCarbohydrate());//碳水化合物
                nutrientTableEntity2.setFat(nutrientTableEntity2.getSaturatedFat() + nutrientTable.getFat());//脂肪
                nutrientTableEntity2.setSugar( nutrientTableEntity2.getSugar() + nutrientTable.getFat());//糖
                nutrientTableEntity2.setSaturatedFat(nutrientTableEntity2.getSaturatedFat() +nutrientTable.getSaturatedFat());//饱和脂肪
                nutrientTableEntity2.setTransFat(nutrientTableEntity2.getTransFat() +nutrientTable.getTransFat());//反式脂肪
                nutrientTableEntity2.setCholesterol(nutrientTableEntity2.getCholesterol() +nutrientTable.getCholesterol());//胆固醇
                nutrientTableEntity2.setSodium(nutrientTableEntity2.getSodium() +nutrientTable.getSodium());//钠

            }
            if (nutrientTableEntity2.getFat() == null){
                return R.error(99,"无今日内容");
            }

////            //与标准的人体所需对比
//            String s = CompareUtils.compareNumberFields(nutrientTableEntity, nutrientTableEntity2);
//            ArrayList<PersonDishEntity> personDishEntities = new ArrayList<>();
//            personDish.setNt(s);
//            personDishEntities.add(personDish);
            List<NutrientTableEntity> nutrientTableEntities = Arrays.asList(nutrientTableEntity2);
            page.setList(nutrientTableEntities);
            page.setTotalCount(nutrientTableEntities.size());
        }


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("menu:persondish:info")
    public R info(@PathVariable("id") Integer id){
		PersonDishEntity personDish = personDishService.getById(id);

        return R.ok().put("personDish", personDish);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("menu:persondish:save")
    public R save(@RequestBody PersonDishEntity personDish){
		personDishService.save(personDish);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("menu:persondish:update")
    public R update(@RequestBody PersonDishEntity personDish){
		personDishService.updateById(personDish);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("menu:persondish:delete")
    public R delete(@RequestBody Integer[] ids){
		personDishService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


}
